package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 处理跨域问题
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 配置跨域
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");    // 允许哪些头进行跨域
        corsConfiguration.addAllowedMethod("*");    // 允许哪些方法进行跨域
        corsConfiguration.addAllowedOriginPattern("*");    // 允许哪些来源进行跨域
        corsConfiguration.setAllowCredentials(true);    // 是否携带cookie进行跨域   否则会丢失 Cookie
        // 哪些路径要配置跨域？？？
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source) ;
    }
}
